﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgramMain.Framework;

namespace ProgramMain.OpticNet
{
    public partial class OpticNetDesignPanel : UserControl
    {
        public OpticNetDesignPanel()
        {
            InitializeComponent();

            labelActionModeCaption.Text = "";
            labelRulerCaption.Text = "";
            checkTranzit.Checked = Properties.Settings.Default.ShowTransitVertexes;
        }

        public int level
        {
            get
            {
                return zoomLevel.Value;
            }
            set
            {
                if (zoomLevel.Value != value)
                {
                    zoomLevel.Value = value;
                }
            }
        }

        public int levelMin
        {
            get
            {
                return zoomLevel.Properties.Minimum;
            }
        }

        public int levelMax
        {
            get
            {
                return zoomLevel.Properties.Maximum;
            }
        }

        public class LevelValueArgs : EventArgs
        {
            public int Level { get; private set; }

            public LevelValueArgs(int level)
            {
                this.Level = level;
            }
        }
        public event EventHandler<LevelValueArgs> LevelValueChanged;

        public event EventHandler CenterMapClicked;

        public event EventHandler RefreshMapClicked;

        public event EventHandler ShowTranzitChange;

        public enum TMouseActionMode { None, MoveVertexMode, CableTunnelMode, RulerMode };

        public enum TMousePopUpMode { None, Normal, Minimize }

        public class ActionModeEventArgs : EventArgs
        {
            public TMouseActionMode MouseActionMode { get; private set; }
            public TMousePopUpMode MousePopUpMode { get; private set; }
            public ActionModeEventArgs(TMouseActionMode mouseActionMode, TMousePopUpMode mousePopUpMode)
            {
                MouseActionMode = mouseActionMode;
                MousePopUpMode = mousePopUpMode;
            }
        }

        public event EventHandler<ActionModeEventArgs> MouseActionModeChanged;
        public event EventHandler<IntEvent> RulerTypeChanged;

        private bool lockEventHandler = false;

        private void zoomLevel_ValueChanged(object sender, EventArgs e)
        {
            if (LevelValueChanged != null && zoomLevel.Value >= levelMin && zoomLevel.Value <= levelMax)
            {
                LevelValueChanged(this, new LevelValueArgs(zoomLevel.Value));
            }
        }

        private void btnCenterMap_Click(object sender, EventArgs e)
        {
            if (CenterMapClicked != null)
                CenterMapClicked(this, e);
        }

        private void btnAddVertex_MouseClick(object sender, MouseEventArgs e)
        {
            MapDragObject.StartNewVertexDragDrop(this);
        }

        private void btnRefreshMap_Click(object sender, EventArgs e)
        {
            if (RefreshMapClicked != null)
                RefreshMapClicked(this, e);
        }

        private void OpticNetDesignPanel_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            if (btnMoveVertexMode.Down)
            {
                MapDragObject.ChangeCursor(e, MapDragObject.DragObjectTypes.Vertex);
            }
        }

        private void btnMoveVertexMode_DownStateValueChanged(object sender, Framework.StateButton.DownStateArgs e)
        {
            if (!Framework.BaseSecurity.CheckSecurityTechAction()) return;
            TrackMouseActionMode(TMouseActionMode.MoveVertexMode, TMousePopUpMode.None);
        }

        private void TrackMouseActionMode(TMouseActionMode bMouseActionMode, TMousePopUpMode mousePopUpMode)
        {
            if (lockEventHandler) return;

            try
            {
                lockEventHandler = true;
                switch (bMouseActionMode)
                {
                    case TMouseActionMode.MoveVertexMode:
                        labelActionModeCaption.Text = btnMoveVertexMode.Down
                                                          ? "Включен режим перемещения объектов колодцев"
                                                          : "";
                        break;
                    case TMouseActionMode.CableTunnelMode:
                        labelActionModeCaption.Text = CableTunnelMode
                                                          ? "Включен режим прокладки туннелей"
                                                          : "";
                        break;
                    case TMouseActionMode.RulerMode:
                        labelRulerCaption.Text = btnRuler
                                                          ? "Включен режим линейки"
                                                          : "";
                        break;
                }
                

                if (MouseActionModeChanged != null)
                    MouseActionModeChanged(this, new ActionModeEventArgs(bMouseActionMode, mousePopUpMode));
            }
            finally
            {
                lockEventHandler = false;
            }
        }

        public void PushUpButtons()
        {
            if (lockEventHandler) return;

            try
            {
                lockEventHandler = true;

                btnMoveVertexMode.Down = false;
                CableTunnelMode = false;

                labelActionModeCaption.Text = "";
            }
            finally
            {
                lockEventHandler = false;
            }
        }

        private void checkTranzit_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.ShowTransitVertexes = checkTranzit.Checked;
            if (ShowTranzitChange != null) ShowTranzitChange(checkTranzit.Checked, EventArgs.Empty);
        }

        private void OpticNetDesignPanel_Load(object sender, EventArgs e)
        {
            if (ShowTranzitChange != null) ShowTranzitChange(checkTranzit.Checked, EventArgs.Empty);
        }

        private void radioRulerTypeSelect_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                int i = (int) radioRulerTypeSelect.EditValue;
                if(RulerTypeChanged != null) RulerTypeChanged(this, new IntEvent(i));
            }
            catch (Exception)
            {
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchMapCtl searchMapCtl = new SearchMapCtl();
            searchMapCtl.ShowModal();//(BaseModalDetailCtl.ShowModeTypes.Edit);

        }

        public event EventHandler ClearMeasureEvent;
        private void ClearMeasure_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ClearMeasureEvent != null) ClearMeasureEvent(this, EventArgs.Empty);
        }

        private bool btnRuler = false;
        private void btnRulerMode_Click(object sender, EventArgs e)
        {
            btnRuler = !btnRuler;
            TrackMouseActionMode(TMouseActionMode.RulerMode, TMousePopUpMode.None);
        }

        public bool SetCheckCableTunnels
        {
            set { CheckCableTunnels.Checked = value; }
        }
        private void CheckCableTunnels_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(CheckCableTunnels.Checked)
                TrackMouseActionMode(TMouseActionMode.CableTunnelMode, TMousePopUpMode.Normal);
            else
                TrackMouseActionMode(TMouseActionMode.CableTunnelMode, TMousePopUpMode.Minimize);
        }

        private bool CableTunnelMode = false;
        private void dropDownButton1_Click(object sender, EventArgs e)
        {
            if (!Framework.BaseSecurity.CheckSecurityTechAction()) return;
            CableTunnelMode = !CableTunnelMode;
          //  btnMoveVertexMode.Enabled = !CableTunnelMode;
            TrackMouseActionMode(TMouseActionMode.CableTunnelMode, TMousePopUpMode.None);
        }

      
       }
}
